package single_game.logic.race_simulator.race_team;

import java.util.ArrayList;
import single_game.logic.race_simulator.race_team.rider_actions.InRelayQueue;
import single_game.logic.race_simulator.race_team.rider_actions.MaintainPosition;
import single_game.logic.race_simulator.race_team.rider_actions.Relay;
import single_game.logic.race_simulator.race_team.rider_actions.SlowDown;
import single_game.logic.race_simulator.race_team.rider_actions.StayBehind;
import main.CyclingManager;
import data.Cyclist;

public class RaceTeamSimulator {
	
	private ArrayList<RaceRider> team;
	private ArrayList<Cyclist> cyclists;
	
	public RaceTeamSimulator(ArrayList<RaceRider> team) {
		this.team = team;
		cyclists = CyclingManager.getInstance().cyclists;
		
		setRidersRole();
	}

	
	public void makeDecision() {
		
		for (int i = 0; i < team.size(); i++) {
			if (team.get(i).getAction().equals("Maintain Position")) {
				MaintainPosition.simulateRiderAction(team.get(i));
			}
			else if (team.get(i).getAction().equals("Relay Queue")) {
				InRelayQueue.simulateRiderAction(team.get(i));
			}
			else if (team.get(i).getAction().equals("Relay")) {
				Relay.simulateRiderAction(team.get(i));
			}
			else if (team.get(i).getAction().equals("Stay Behind")) {
				StayBehind.simulateRiderAction(team.get(i));
			}
			else if (team.get(i).getAction().equals("Slow Down")) {
				SlowDown.simulateRiderAction(team.get(i));
			}
		}		
	}
	
	public void newStage() {
		double startPuls = 120;
		double stamina = 100;
		double energy = 100;
		String action = "Maintain Position";
		
		for (int i = 0; i < team.size(); i++) {
			team.get(i).setPuls(startPuls);
			team.get(i).setStamina(stamina);
			team.get(i).setEnergy(energy);
			team.get(i).setAction(action);
			team.get(i).setPower(50);
		}
	}
	
	private void setRidersRole() {
		
		for (int i = 0; i < team.size(); i++) {
			if (cyclists.get(team.get(i).getCyclistId()).getSpecial().equals("Sprinter")) {
				team.get(i).setRiderRole("Sprinter");
			}
			else if (isMost(i)) {
				team.get(i).setRiderRole("Leader");
			}
			else if (cyclists.get(team.get(i).getCyclistId()).getMountain() >= 76 || cyclists.get(team.get(i).getCyclistId()).getFlat() >= 78) {
				team.get(i).setRiderRole("Free Rider");
			}
			else {
				team.get(i).setRiderRole("Team Mate");
			}
		}		
	}
	
	
	private Boolean isMost(int index) {
		
		for (int j = 0; j < team.size(); j++) {
			if (cyclists.get(team.get(index).getCyclistId()).getMountain() < cyclists.get(team.get(j).getCyclistId()).getMountain()) {
				return false;
			}
		}
		return true;
	}

}
